package com.ruoyi.project.cms.type.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.cms.type.mapper.CmsTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CMSTypeServiceImpl implements CMSTypeService {
    //声明mapper层属性
    @Autowired
    private CmsTypeMapper cmsTypeMapper;
    //删除公告类型
    @Override
    public int deleteTypeByIds(String ids) {
        Long[] typeIds = Convert.toLongArray(ids);

       /* for (Long typeId : typeIds)
        {
            //判断要删除的公告类型是否已经存在对应的公告信息
            *//*Post post = selectPostById(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }*//*
        }*/

        return cmsTypeMapper.deleteTypeByIds(typeIds);
    }

    //保存修改公告类别
    @Override
    public int updateCmsType(CmsType cmsType) {
        cmsType.setUpdateBy(ShiroUtils.getLoginName());
        return cmsTypeMapper.updateCmsTypeMapper(cmsType);
    }

    //获取要修改的公告信息
    @Override
    public CmsType selectTypeById(Long typeId) {
        return cmsTypeMapper.selectTypeByIdMapper(typeId);
    }

    //公告类型的新增
    @Override
    public int insertCmsType(CmsType cmsType) {
        cmsType.setCreateBy(ShiroUtils.getLoginName());
        return cmsTypeMapper.insertCmsType(cmsType);
    }

    //校验类型名称
    @Override
    public String checkTypeNameUnique(CmsType cmsType) {
        Long typeId = StringUtils.isNull(cmsType.getTypeId()) ? -1L : cmsType.getTypeId();
        CmsType info = cmsTypeMapper.checkTypeNameUnique(cmsType.getTypeName());
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue())
        {
            return UserConstants.TYPE_NAME_NOT_UNIQUE;
        }
        return UserConstants.TYPE_NAME_UNIQUE;
    }

    //加载公告类型信息
    @Override
    public List<CmsType> selectCmsTypeList(CmsType cmsType) {

        return cmsTypeMapper.selectCmsTypeListMapper(cmsType);
    }
}
